package com.zi.experttalk.experttalk.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.zi.experttalk.experttalk.Model.service.HttpManager;
import com.zi.experttalk.experttalk.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/16.
 */
public class CouponCommentActivity extends Activity {
    private Button coupon_comment_back_bt;
    private Button coupon_comment_addComment_bt;
    List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();
    private ListView coupon_comment_listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.coupon_comment);
     //取键
        coupon_comment_back_bt=(Button)findViewById(R.id.coupon_comment_back_bt);
        coupon_comment_addComment_bt= (Button)findViewById(R.id.coupon_comment_addComment_bt);
        coupon_comment_listview= (ListView)findViewById(R.id.coupon_comment_listview);

     //评论按键事件
        coupon_comment_back_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        coupon_comment_addComment_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

       /*
       评论列表显示
        */
        Intent intent=getIntent();
        String mId= intent.getStringExtra("youhui_id");
        String yu_id=mId;
        String commentParam="{\"content\":\"\",\"pwd\":\"123456\",\"yh_id\":\""+yu_id+"\",\"act\":\"youhui_comment_list\",\"page\":\"1\",\"email\":\"cdm123\"}";
        HttpManager httpManager=new HttpManager();
        try {
            httpManager.getHttpInfo(commentParam,new HttpManager.GetDataResultListener() {
                @Override
                public void onResult(String data) {
                        if(data!=null){
                            try {
                                showList(data);
                            } catch (JSONException e) {
                                Toast.makeText(CouponCommentActivity.this, "解析有误", Toast.LENGTH_SHORT).show();
                            }
                        }
                }
            });
        } catch (IOException e) {
            Toast.makeText(this, "网络请求失败", Toast.LENGTH_SHORT).show();
        }

    }
    private void  showList(String data) throws JSONException {
        JSONObject comObject=new JSONObject(data);
        JSONArray  comArray=comObject.optJSONArray("item");
        for (int i = 0; i <comArray.length() ; i++) {
            JSONObject itemObject=comArray.optJSONObject(i);
            String userName=itemObject.optString("user_name");
            String createTimeFormat=itemObject.optString("create_time_format");
            String content=itemObject.optString("content");
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("userName",userName);
            map.put("createTimeFormat",createTimeFormat);
            map.put("content",content);
            mapList.add(map);
        }
        //写入Adapter
        final SimpleAdapter simpleAdapter=new SimpleAdapter(CouponCommentActivity.this,
                mapList,
                R.layout.coupon_comment_list_item,
                new String[]{"userName","createTimeFormat","content"},
                new int[]{R.id.coupon_commentList_userName,R.id.coupon_commentList_creatTime,R.id.coupon_commentList_content});
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                coupon_comment_listview.setAdapter(simpleAdapter);
            }
        });

    }
}
